package cn.chia.back.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.back.dao.IActivityDAO;
import cn.chia.pojo.TblActivity;
@Component
public class ActivityDAOImpl extends HibernateDaoSupport implements IActivityDAO{

	@Autowired
	public ActivityDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	
	/**
	 * @author Administrator
	 * @param vo 要添加的TblActivity类。
	 * @Todo 添加。
	 * @throws Exception
	 */
	public void doCreate(TblActivity vo) throws Exception {
		this.getHibernateTemplate().save(vo);
	}


	/**
	 * @author Administrator
	 * @param id TblActivity类的id。
	 * @Todo 删除。
	 * @throws Exception
	 */
	public void doRemove(Integer id) throws Exception {
		String hql = "DELETE FROM TblActivity AS p WHERE p.id=?";
		Query query = getSession().createQuery(hql);
		query.setInteger(0,id);
		query.executeUpdate();	
	}

	/**
	 * @author Administrator
	 * @param vo 要修改的TblActivity实体类。
	 * @Todo 修改。
	 * @throws Exception
	 */
	public void doUpdate(TblActivity vo) throws Exception {
		this.getHibernateTemplate().update(vo);
	}

	
	public  List<TblActivity> findByType(Integer typeId)throws Exception{
		return null;
	}
	
	
	
	/**
	 * @author xyh
	 * @Todo query all activity(查询全部活动)
	 * @return conllection of all activity （返回全部活动的list集合）
	 */
	public List<TblActivity> findAll(int pageNo, int pageSize, String keyword,
			String column) throws Exception {
		// 通过一个动态的column,设置要按照哪个属性来查询
		String hql = "FROM TblActivity AS p WHERE p." + column + " LIKE ?";
		Query query = getSession().createQuery(hql);
		// 设置参数
		query.setString(0, "%" + keyword + "%");
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * @author xyh
	 * @Todo obtain Total record number,In order to used the page(获取全部记录数，为了分页)
	 * @return int (the number of pages)（返回一个int类型的记录数）
	 */
	public int getAllCount(String keyword, String column) throws Exception {
		// 查询的不是pojo对象时,需要编写SELECT语句
		String hql = "SELECT COUNT(p) FROM TblActivity AS p WHERE p." + column + " LIKE ?";
		Query query = getSession().createQuery(hql);
		query.setString(0, "%" + keyword + "%");
		return ((Long) query.uniqueResult()).intValue();
	}

	public List<TblActivity> findAll() throws Exception {return null;}

	public TblActivity findById(Integer id) throws Exception {
		return  (TblActivity) getSession().get(TblActivity.class, id);
	}
	
	
}
